package com.jy.adscm.zltx.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.QsAttachment;
import com.jy.adscm.zltx.service.QsAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-11-07
 */
@RestController
@RequestMapping("/adscm/zltx/qsAttachment")
public class QsAttachmentController {
    @Value("${zltx.qsFile.upload.annex-upload-path}")
    private  String annexUploadPath;//附件上传路径
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    QsAttachmentService qsAttachmentService;
     @RequestMapping("upload")
     public SimpleCoreResponse uploadQsAttachment(HttpServletRequest request) throws AdscmSystemException, IOException {
         String tokenId = request.getParameter("tokenId");
         String qsAttachmentstr = request.getParameter("qsAttachment");
         if (qsAttachmentstr==null||"".equals(qsAttachmentstr)){
             throw  new AdscmSystemException("qsAttachmentstr 没得");
         }
         ObjectMapper mapper = new ObjectMapper();
         QsAttachment qsAttachment = mapper.readValue(qsAttachmentstr,QsAttachment.class);
         Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
         List<String> paths = null;
         CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                 request.getSession().getServletContext());
         if(multipartResolver.isMultipart(request)){
             //将request变成多部分request
             MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

             String path = annexUploadPath /*+ File.separator*/ + UUID.randomUUID().toString().replaceAll("-","") + File.separator;

             paths = AdscmSystemUtils.getInstance().uploadFileNoNewName(multiRequest,path);
         }
         if (paths!=null){
             String path = paths.get(0);
             qsAttachment.setFilePath(path);
             String  fileName =  path.substring(path.lastIndexOf("\\") + 1);
             qsAttachment.setFileName(fileName);
             qsAttachment.setUploadTime(new Date());
             qsAttachment.setUploadPerson(token.getUser().getId().toString());
         }

         return new SimpleCoreResponse("200","上传成功",qsAttachmentService.insert(qsAttachment));
     }
    @RequestMapping("download/{id}")
    public  SimpleCoreResponse downloadQsAttachment(@PathVariable String id, HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException{
        QsAttachment qsAttachment = qsAttachmentService.selectById(id);
        if (qsAttachment==null){
            throw  new AdscmSystemException("没找到文件");
        }
        AdscmSystemUtils.getInstance().downloadFile(qsAttachment.getFilePath(),response);
        return new SimpleCoreResponse("200","下载成功",null);
    }
    @RequestMapping("selectByFileId/{fileId}")
    public  SimpleCoreResponse  selectQsAttachments(@PathVariable Integer fileId)throws AdscmSystemException{

        List<QsAttachment> qsAttachments = qsAttachmentService.selectQsAttachments(fileId);
        return  new SimpleCoreResponse("200","查询成功",qsAttachments);
    }
}

